<template>
  <div style="position: relative;">
    <el-form :inline="true" size="mini" :model="formInline" label-position="right">
      <el-form-item label="考勤时间">
        <el-date-picker v-model="formInline.clockMonth" type="month" placeholder="选择月"
                        format="yyyy-MM" value-format="yyyy-MM"></el-date-picker>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input class="filter-item" v-model="formInline.userName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="部门">
        <el-select v-model="formInline.deptId" placeholder="请选择">
          <el-option v-for="(item,index) in dropdownList" :key="index"
                     :label="item.deptName" :value="item.deptId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="考勤组">
        <el-select v-model="formInline.groupId" placeholder="请选择">
          <el-option v-for="(item,index) in grouplist" :key="index"
                     :label="item.groupName" :value="item.groupId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" @click="onSubmit">搜索</el-button>
        <el-button type="warning" class="el-button--reset" icon="el-icon-refresh-right" @click="resetFromdata" size="mini"></el-button>
        <el-button type="primary" size="mini" @click="exportExcelData">记录导出</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="24">
        <el-table :data="tableData" size="mini" style="width: 100%;" header-cell-class-name="table-header-gray" v-loading="isShowloading">
          <el-table-column type="index" align="center" width="55" label="序号"></el-table-column>
          <el-table-column prop="userId" align="center" label="员工编号" show-overflow-tooltip></el-table-column>
          <el-table-column prop="userName" align="center" label="姓名" show-overflow-tooltip></el-table-column>
          <el-table-column prop="deptName" align="center" label="部门" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="groupName" align="center" label="考勤组" show-overflow-tooltip></el-table-column>
          <el-table-column prop="averageWorkHouses" align="center" label="平均工时" show-overflow-tooltip></el-table-column>
          <el-table-column prop="attendanceDays" align="center" label="出勤天数" show-overflow-tooltip></el-table-column>
          <el-table-column prop="restDays" align="center" label="休息天数" show-overflow-tooltip></el-table-column>
          <el-table-column prop="lateDesc" align="center" label="迟到" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.lateTimes}}次，共{{scope.row.lateMinutes}}分钟</span>
            </template>
          </el-table-column>
          <el-table-column prop="leaveEarlyTimes" align="center" label="早退" show-overflow-tooltip></el-table-column>
          <el-table-column prop="absenteeismDays" align="center" label="旷工" show-overflow-tooltip></el-table-column>
          <el-table-column prop="fieldWorkTimes" align="center" label="外勤" show-overflow-tooltip></el-table-column>
          <el-table-column prop="missingClockTimes" align="center" label="缺卡" show-overflow-tooltip></el-table-column>
          <el-table-column prop="workOverHours" align="center" label="加班" show-overflow-tooltip></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="text" class="table-btn success" @click="handleRecord(scope.row)">记录</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="end" style="margin-top: 10px;">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNo"
            :page-size="page_size"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            :total="total"></el-pagination>
        </el-row>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { AttendanceStatistics, AttendanceSettings } from '@/api/attenDances.js';
import { SysDeptController } from '@/api';
import AttendanceRecord from './AttendanceRecord.vue';
export default {
  name: 'AttendanceStatistics',
  data () {
    return {
      editDialogVisible: false,
      formInline: {
        clockMonth: '2022-01',
        deptId: null,
        groupId: null,
        userName: null
      },
      showDetail: {},
      showEdit: {},
      tableData: [],
      villageData: [],
      total: 0,
      page_size: 10,
      pageNo: 1,
      isShowloading: false,
      Addroptions: [],
      defaultProps: {
        children: 'nodes',
        label: 'name'
      },
      dropdownList: [],
      grouplist: []
    }
  },
  created () {
  },
  mounted () {
    this.loadTimeWidgetData()
    this.loadDeptmentDropdownList()
    this.loadGroupWidgetData()
  },
  computed: {},
  methods: {
    /**
     * 默认时间
     */
    loadTimeWidgetData () {
      let yearinfo = Number(this.getDateTime.dateTimeStr('y'))
      let monthinfo = Number(this.getDateTime.dateTimeStr('m'))
      if (monthinfo === 12) {
        yearinfo = yearinfo - 1
        monthinfo = monthinfo - 1
      } else {
        monthinfo = monthinfo - 1
      }
      if (monthinfo < 10) {
        monthinfo = '0' + monthinfo
      }
      this.formInline.clockMonth = yearinfo + '-' + monthinfo
    },
    /**
     * 考勤统计
     */
    loadAttendanceWidgetData () {
      let params = {
        ...this.formInline,
        size: this.page_size,
        current: this.pageNo
      }
      return new Promise((resolve, reject) => {
        AttendanceStatistics.statisticsByGroup(this, params).then(res => {
          this.tableData = res.data.records
          this.total = res.data.total
        }).catch(e => {
          reject(e);
        });
      });
    },
    /**
     * 考勤设置列表
     */
    loadGroupWidgetData () {
      let params = {
        size: 1000,
        current: 1
      }
      return new Promise((resolve, reject) => {
        AttendanceSettings.list(this, params).then(res => {
          this.grouplist = res.data.records
          if (this.grouplist.length !== 0) {
            this.formInline.groupId = this.grouplist[0].groupId
          }
          this.loadAttendanceWidgetData()
        }).catch(e => {
          reject(e);
        });
      });
    },
    /**
     * 所属部门下拉数据获取函数
     */
    loadDeptmentDropdownList () {
      return new Promise((resolve, reject) => {
        let params = {};
        SysDeptController.list(this, params).then(res => {
          this.dropdownList = res.data.dataList;
        }).catch(e => {
          reject(e);
        });
      });
    },
    // 记录
    exportExcelData () {
      let params = {
        ...this.formInline,
        size: this.page_size,
        current: this.pageNo
      }
      return new Promise((resolve, reject) => {
        AttendanceStatistics.statisticsExportByGroup(this, params, '考勤统计.xlsx')
      });
    },
    handleRecord (row) {
      this.$dialog.show('记录', AttendanceRecord, {
        area: ['800px', '65vh'],
        offset: '100px'
      }, {
        clockMonth: this.formInline.clockMonth,
        userId: row.userId
      }).then(res => {
        this.loadAttendanceWidgetData()
      }).catch(e => {
      });
    },
    // 搜索
    onSubmit () {
      this.pageNo = 1
      this.loadAttendanceWidgetData()
    },
    // 重置
    resetFromdata () {
      this.formInline = {
        clockMonth: null,
        deptId: null,
        groupId: this.grouplist[0].groupId,
        userName: null
      }
      this.loadTimeWidgetData()
      this.loadAttendanceWidgetData()
    },
    // 分页导航
    handleCurrentChange (val) {
      this.pageNo = val
      this.loadAttendanceWidgetData()
    },
    handleSizeChange (val) {
      this.page_size = val
      this.loadAttendanceWidgetData()
    },
    currentChange (val) {
      this.pageNo = val
      this.loadAttendanceWidgetData()
    },

    // 查看
    handleInfo (data) {
    }
  }
}
</script>
